package com.feishi.core.util.bitbyte;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: caixq
 * Date: 2019-06-18
 * Time: 上午10:46
 */
public class TimeConvert {
    public static void main(String[] args){


        Date date=new Date();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        System.out.println(Integer.MAX_VALUE-System.currentTimeMillis()/1000);
        System.out.println(String.format("%X",System.currentTimeMillis()/1000));
        System.out.println(System.currentTimeMillis()/1000);
        System.out.println(Integer.MAX_VALUE);

        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        System.out.println(getDays(1970));
        for (int i = 0; i < 150; i++) {

            calendar.set(Calendar.MONTH,i%12);
            calendar.set(Calendar.YEAR,2016+i/12);

            int month=calendar.get(Calendar.MONTH);

            int day=calendar.get(Calendar.DATE);
            int year=calendar.get(Calendar.YEAR);
            int hour=calendar.get(Calendar.HOUR_OF_DAY);
            int minute=calendar.get(Calendar.MINUTE);
            int second=calendar.get(Calendar.SECOND);
            int milliSecond=calendar.get(Calendar.MILLISECOND);
            long t=getTime(year,month,day,hour,minute,second,milliSecond);
            if(((calendar.getTime().getTime()-t)/1000/60/60/24)!=0){
                System.out.println(calendar.getTime().getTime());
                System.out.println(t);
                System.out.println((calendar.getTime().getTime()-t)/1000/60/60/24);
                System.out.println(calendar.getTime().toLocaleString());
                System.out.println("-------------------------");

            }
        }



    }

    private static long getTime(int year, int month, int day, int hour, int minute, int second, int milliSecond) {
        month=month-2;

        if(month<0){
            month+=12;
            year--;
        }

      int year0=1970;



        long mday=(367*month+7)/12;

        int removeDay=365-31-28;
        long time=(((getDays(year)-removeDay-getDays(year0)+mday + day-1)*24+hour)*60+minute)*60+second;

        //time=((((year*365+(year-1)/4+1-(year-1)/100+(year-1)/400+mday + day-719835)*24+hour)*60+minute)*60+second)*1000+milliSecond;

        time=((((year*365l+year/4-year/100+year/400+(367*month+7)/12 + day-719469
        )*24+hour
        )*60+minute
        )*60+second
        )*1000+milliSecond;


        time=((((year*365l+year/4-year/100+year/400+ 30*month+(month+1)*7/12 + day-719469
        )*24+hour
        )*60+minute
        )*60+second
        )*1000+milliSecond;
        //令
        month=month+1;

        time=((((year*365l+year/4-year/100+year/400+ 30*month+month*7/12 -30 + day-719469
        )*24+hour
        )*60+minute
        )*60+second
        )*1000+milliSecond;

        time=((((year*365l+year/4-year/100+year/400+ month*367/12  + day-719499
        )*24+hour
        )*60+minute
        )*60+second
        )*1000+milliSecond;


        return time;
    }

    private static long getDays(int year) {
        return year*365+(year-1)/4+1-(year-1)/100+(year-1)/400 ;
    }

    public static long getTime1(int year, int month, int day, int hour, int minute, int second, int milliSecond){


             int mon = month+1;
            /* 1..12 -> 11,12,1..10 */
            if (0 >= (int) (mon -= 2)) {
                mon += 12;  /* Puts Feb last since it has leap day */
                year -= 1;
            }

            return ((((year*365l + year/4 - year/100 + year/400 + 367*month/12 + day - 719499
            )*24 + hour /* now have hours */
            )*60 + minute /* now have minutes */
            )*60 + second
            )*1000+milliSecond; /* finally seconds */

    }
}
